package com.supply.dl.activity.seachmode;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.util.ArrayMap;
import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.Toast;

import com.supply.dl.R;
import com.supply.dl.activity.BaseActivity;
import com.supply.dl.activity.FeedOperationDetailActivity;
import com.supply.dl.activity.InAndOutBoundLocationActivity;
import com.supply.dl.activity.InAndOutBoundLocationDetailActivity;
import com.supply.dl.app.ApiService;
import com.supply.dl.app.AppConfig;
import com.supply.dl.app.adapter.NormalAdapter;
import com.supply.dl.app.adapter.ViewHolder;
import com.supply.dl.app.http.BaseInfo;
import com.supply.dl.app.http.BaseObserver;
import com.supply.dl.app.http.HttpMethod;
import com.supply.dl.app.http.RxSchedulers;
import com.supply.dl.bean.BaseItemBean;
import com.supply.dl.bean.InAndOutBoundLocationBean;
import com.supply.dl.bean.request.InBoundLocationReq;
import com.supply.dl.constant.Constants;
import com.supply.dl.http.HttpManger;
import com.supply.dl.utils.DateUtils;
import com.supply.dl.utils.StringUtils;
import com.supply.dl.utils.utils.ToastUtil;
import com.supply.dl.widget.LoadTipFailedLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ${qianzhihe} on 2018/8/4.
 * 入库定位列表
 * 出库定位列表
 */

public class InAndOutBoundLocationModle extends BaseModle {



    @Override
    public void setSearchAdapter() {
        super.setSearchAdapter();
        if (adapter != null)
            return;
        resource = new ArrayList<>();
        adapter = new NormalAdapter<BaseItemBean>(context, resource) {
            @Override
            protected void convert(ViewHolder holder, BaseItemBean item) {
                InAndOutBoundLocationBean inBoundLocationBean = (InAndOutBoundLocationBean) item.object;
                holder.setText(R.id.inbound_location_main_item_purchaseContentTv, inBoundLocationBean.bussTypeName);//订单类型名称
                holder.setText(R.id.inbound_location_main_item_positionStatusName, inBoundLocationBean.positionStatusName);//定库状态名称
                holder.setText(R.id.inbound_location_main_item_purchasePersonNameTv, inBoundLocationBean.driverName);//驾驶员姓名
                holder.setText(R.id.inbound_location_main_item_carIdCardTv, inBoundLocationBean.carNo);//车牌号
                holder.setText(R.id.inbound_location_main_item_timeTv, StringUtils.isEmpty(String.valueOf(inBoundLocationBean.addTime)) ? "" : DateUtils.getSimpleDateTimeByMill(String.valueOf(inBoundLocationBean.addTime), DateUtils.DATE_TIME_FORMAT));//建单时间

                holder.setText(R.id.inbound_location_main_item_showERPNo, inBoundLocationBean.erpOrderNo);//ERP订单号

                if (isInBound) {//入库
                    if (!StringUtils.isEmpty(inBoundLocationBean.noticeOrderId)) {//通知单
                        holder.setText(R.id.inbound_location_main_item_inboundNoticeLetterNo, getResourcesString(R.string.inboundNoticeLetterNo));
                    } else {//订单
                        holder.setText(R.id.inbound_location_main_item_inboundNoticeLetterNo, "入库订单号");
                    }
                } else {//出库
                    if (!StringUtils.isEmpty(inBoundLocationBean.noticeOrderId)) {//通知单
                        holder.setText(R.id.inbound_location_main_item_inboundNoticeLetterNo, getResourcesString(R.string.outboundNoticeLetterNo));
                    } else {//订单
                        holder.setText(R.id.inbound_location_main_item_inboundNoticeLetterNo, "出库订单号");
                    }
                }

                if (!StringUtils.isEmpty(inBoundLocationBean.noticeOrderId)) {//通知单
                    holder.setText(R.id.inbound_location_main_item_purchaseTypeTv, getResourcesString(R.string.noticeLetter));
                    holder.setBackgroundRes(R.id.inbound_location_main_item_purchaseTypeTv, R.drawable.shape_notice_purple_bg);
                    holder.setText(R.id.inbound_location_main_item_showInboundNoticeLetterNo, inBoundLocationBean.noticeOrderNo);//通知单号
                } else {//订单
                    holder.setText(R.id.inbound_location_main_item_purchaseTypeTv, getResourcesString(R.string.orderLetter));
                    holder.setBackgroundRes(R.id.inbound_location_main_item_purchaseTypeTv, R.drawable.shape_order_purple_bg);
                    holder.setText(R.id.inbound_location_main_item_showInboundNoticeLetterNo, inBoundLocationBean.orderNo);//订单号
                }
                holder.getView(R.id.inbound_location_main_item_lyParent).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                    }
                });
            }

            @Override
            protected int getItemLayoutId() {
                return R.layout.inbound_location_main_item;
            }
        };
        adapter.setOnItemClickListener(this);
    }

    /**
     * item点击事件
     */
    @Override
    public void onItemClick(ViewHolder viewHolder, BaseItemBean itemBean, int position) {
        super.onItemClick(viewHolder, itemBean, position);
        Bundle bundle = new Bundle();
        if (isInBound){
            bundle.putInt(Constants.FLAG_ACTIVITY_KEY, Constants.FLAG_ACTIVITY_IN_BOUND_LOCATION_DETAIL_UI);
        }else {
            bundle.putInt(Constants.FLAG_ACTIVITY_KEY, Constants.FLAG_ACTIVITY_OUT_BOUND_LOCATION_DETAIL_UI);
        }
        bundle.putSerializable("InAndOutBoundLocationBean",  (InAndOutBoundLocationBean)itemBean.object);
        ((BaseActivity) context).jumpActivity(InAndOutBoundLocationDetailActivity.class, bundle);
    }

    @Override
    public void setHintText() {
        super.setHintText();
        hintText = getResourcesString(R.string.hintSearchConditionForInBoundLocation);
    }

    /**
     * 设置标题内容
     * FLAG_ACTIVITY_IN_BOUND_LOCATION_UI：入库定位列表
     * FLAG_ACTIVITY_OUT_BOUND_LOCATION_UI：出库定位列表
     */
    @Override
    public void setTitleShow() {
        super.setTitleShow();
        if (mActivityFlag==Constants.FLAG_ACTIVITY_IN_BOUND_LOCATION_UI){
            isInBound=true;
            titleShow=getResourcesString(R.string.inbound_location);
        }else if (mActivityFlag==Constants.FLAG_ACTIVITY_OUT_BOUND_LOCATION_UI){
            isInBound=false;
            titleShow=getResourcesString(R.string.outbound_location);
        }
    }

    @Override
    public void init(Context context) {
        super.init(context);
        this.context = context;
        setTitleShow();
        setSearchAdapter();
    }

    @Override
    public void vistData(String keyWord, ArrayMap<String, String> filterMap, OnVistDataSuccess onVistDataSuccess) {
        super.vistData(keyWord, filterMap, onVistDataSuccess);
        getInOrOutBoundLocationData(keyWord, filterMap);
        if (onVistDataSuccess != null) {
            onVistDataSuccess.showUi(resource.size() == 0);
        }
    }



    /**
     * 获取入库定位列表数据
     * 获取出库定位列表数据
     */
    private void getInOrOutBoundLocationData(String keyWord,  ArrayMap<String, String> filterMap){
        String inBoundStatus="";//入库状态
        String bussType="";//业务类型
        String companyId="";//公司
        String factoryId="";//工厂
        String productWorkshopId="";//车间
        String createTimeStart="";//建单开始时间
        String createTimeEnd="";//建单结束时间
//        String pageNum = "1";
        if (filterMap.size()>0){
            //取用户选择的入库状态
            inBoundStatus = filterMap.get(AppConfig.SearchFilterAction.IN_HOUSER_STATUS)==null ? "": filterMap.get(AppConfig.SearchFilterAction.IN_HOUSER_STATUS);
            bussType = filterMap.get(AppConfig.SearchFilterAction.BUSINESS_TYPE)==null ? "": filterMap.get(AppConfig.SearchFilterAction.BUSINESS_TYPE);
            companyId = filterMap.get(AppConfig.SearchFilterAction.COMPANY)==null ? "": filterMap.get(AppConfig.SearchFilterAction.COMPANY);
            factoryId = filterMap.get(AppConfig.SearchFilterAction.FACTORY)==null ? "": filterMap.get(AppConfig.SearchFilterAction.FACTORY);
            productWorkshopId = filterMap.get(AppConfig.SearchFilterAction.WORKSHOP)==null ? "": filterMap.get(AppConfig.SearchFilterAction.WORKSHOP);
            createTimeStart = filterMap.get(AppConfig.SearchFilterAction.START_TIME)==null ? "": filterMap.get(AppConfig.SearchFilterAction.START_TIME);
            createTimeEnd = filterMap.get(AppConfig.SearchFilterAction.END_TIME)==null ? "": filterMap.get(AppConfig.SearchFilterAction.END_TIME);
//            pageNum = filterMap.get("pageNum")==null ? "1": filterMap.get("pageNum");
        }
        HttpManger httpManger=HttpManger.getInstance();
        httpManger.setContext(context);
        InBoundLocationReq req = new InBoundLocationReq();
        req.key=keyWord;//搜索关键字
        req.positionStatus=inBoundStatus;
        req.bussType=bussType;
        req.companyId=companyId;
        req.factoryId=factoryId;
        req.productWorkshopId=productWorkshopId;
        req.createTimeStart=createTimeStart;
        req.createTimeEnd=createTimeEnd;
        if(isInBound){
            httpManger.getInboundLocationData(req, String.valueOf(pageNum));
        }else {
            httpManger.getOutboundLocationData(req, String.valueOf(pageNum));
        }
        httpManger.setHttpResponseCallBackListener(this);
    }

    /**
     * 入库定位、出库定位接口数据回调成功
     * @param baseInfo
     */
    @Override
    public void onSuccess(BaseInfo<?> baseInfo) {
        super.onSuccess(baseInfo);
        List<InAndOutBoundLocationBean> dataList= (List<InAndOutBoundLocationBean>) baseInfo.getData();
        if(pageNum == 1){
            resource.clear();
        }
        if(dataList.size() < 10){
            hasMoreData = false;
        }
        if (dataList != null && dataList.size()>0) {
            for (InAndOutBoundLocationBean locationBean : dataList){
                resource.add(new BaseItemBean(locationBean));
            }
        }
        adapter.notifyDataSetChanged();
    }

    /**
     * 入库定位、出库定位接口数据回调失败
     * @param message
     */
    @Override
    public void onFailure(String message) {
        super.onFailure(message);
        ToastUtil.showToast(context, message);
    }
}
